package wsz;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) throws Exception {
        String res = encrypt("123", "b8ffd8fdb02a7738590980783c5630f73d2b1e7150c89240b859e0969ab40a0f");
        System.out.println(res);
        String res1 = encrypt("123", "46EBA22EF5204DD5B110A1F730513961");
        System.out.println(res1);
    }

    public static String encrypt(String value, String key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(hex(key.toCharArray()), "AES"));
        byte[] bytes1 = cipher.doFinal(value.getBytes());
        return Base64.encodeBase64String(bytes1);
    }

    public static byte[] hex(char[] hex) {
        int len = hex.length / 2;
        byte[] raw = new byte[len];

        for (int i=0; i < len; i++) {
            int high = Character.digit(hex[i*2], 16);
            int low = Character.digit(hex[i*2 + 1], 16);
            int value = (high << 4) | low;
            if (value > 127) {
                value -= 256;
            }
            raw[i] = (byte) value;
        }
        return raw;
    }
}
